package com.liusy.serachengine.parserFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;


import org.apache.log4j.Logger;
import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.xmlbeans.XmlException;

public class ParserPpt {

	private static Logger log=Logger.getLogger(ParserPpt.class.getName());
	   public static StringBuffer readPpt(String path){
		      StringBuffer content = new StringBuffer("");
		      try {
		      File f = new File(path);
		      if(!f.exists())return content;
		      FileInputStream fis = new FileInputStream(path);
		      if(path.substring(path.lastIndexOf(".")+ 1, path.length()).toLowerCase().equals("pptx"))
		      {
		    	  POITextExtractor extractor = ExtractorFactory.createExtractor(fis);
		    	  content.append(extractor.getText());
		    	  return content;
		      }
		      SlideShow ppt = new SlideShow(new HSLFSlideShow(path)); //��ȡPPT
		      Slide[] slides = ppt.getSlides();
		      //��ȡ�ı���Ϣ		 
		      for (Slide each : slides) {
		         TextRun[] textRuns = each.getTextRuns();
		         for (int i = 0; i < textRuns.length; i++) {
		            RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns();
		            for (int j = 0; j < richTextRuns.length; j++) {
		               content.append(richTextRuns[j].getText() + "  ");
		            }
		         }
		      }
		      fis.close();
		  	} catch (Exception e) {
		  		log.error("parser Ppt error:"+path,e);
		  		System.out.println("��ȡ�ļ�"+path+"���ı����ݳ��?����������δ���ɹ���");
			}
		      return content;
		   }
	   
	   public static void main(String [] arg){
		   
		   try {
			POITextExtractor extractor = ExtractorFactory.createExtractor(new FileInputStream(".\\���ܽ�.pptx"));
		} catch (InvalidFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OpenXML4JException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
	   }

}
